<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 组控制器
 */
class Group extends AdminBase
{

    /**
     * 组列表
     */
    public function groupList()
    {
        error_reporting(E_ERROR | E_PARSE );
        $where = $this->logicGroup->getWhere($this->param);
        $list=$this->logicGroup->getGroupList($where);
        if(IS_POST){
            return json($list);
        }else{
            $this->assign('company_list', $this->logicCompany->getCompanyList());
            if($this->param['company_id']){
                $this->assign('branch_list', $this->logicBranch->getBranchList(['company_id'=>$this->param['company_id']],'a.*,b.name','',false));
            }
            
            foreach ($list as $key => $value) {
                $list[$key]['num']=$this->logicStaff->staffNum('group',$value['id'])['num'];
            }
            $this->assign('list',$list);
            return $this->fetch('group_list');
        }
    }
    
    /**
     * 组导出
     */
    public function exportGroupList()
    {
        $where = $this->logicGroup->getWhere($this->param);
        $this->logicGroup->exportGroupList($where);
    }

    /**
     * 组添加
     */
    public function groupAdd()
    {
        
        IS_POST && $this->jump($this->logicGroup->groupAdd($this->param));
        $this->assign('company_list', $this->logicCompany->getCompanyList());
        $this->assign('branch_list', $this->logicBranch->getBranchList($where = [], $field = ''));
        return $this->fetch('group_add');
    }
    
    /**
     * 组编辑
     */
    public function groupEdit()
    {
        if(IS_POST){
            if($this->param['id'] == '0' || empty($this->param['id'])){
                return [RESULT_ERROR, '数据异常，请联系管理员'];
            }else{
                $this->jump($this->logicGroup->groupEdit($this->param));
            }
            
        }
        
        $this->assign('company_list', $this->logicCompany->getCompanyList());
        $this->assign('branch_list', $this->logicBranch->getBranchList($where = [], $field = ''));
        $info = $this->logicGroup->getGroupInfo(['id' => $this->param['id']]);
    
        $this->assign('info', $info);
        
        return $this->fetch('group_edit');
    }
    
    /**
     * 组删除
     */
    public function groupDel($id = 0)
    {
        
        return $this->jump($this->logicGroup->groupDel(['id' => $id]));
    }
}
